package com.fengke.cn.yejian11.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.fengke.cn.yejian11.R;
import com.fengke.cn.yejian11.config.Config;
import com.fengke.cn.yejian11.impls.ActImpls;
import com.fengke.cn.yejian11.javabean.Pdingdan;
import com.squareup.picasso.Picasso;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class DingDanActivity extends Activity {

    private ImageView p_pic;
    private TextView p_name;
    private TextView p_price;
    private TextView p_traintime;
    private TextView p_place;
    private TextView ddd_ddd;
    private TextView p_num;
    private TextView p_pay;
    private TextView p_dingdan;
    private TextView p_updatetime;
    private Button p_button;
    String p_id;
    String p_type;
    String SessionID;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ding_dan);
        p_type="2";
        SessionID="pf2cmg5jk9p24f2mv1kjf55bo3";
        p_id=getIntent().getStringExtra("DingDan_id");

        p_button= (Button) findViewById(R.id.p_button);
        ddd_ddd= (TextView) findViewById(R.id.ddd_ddd);
        p_pic= (ImageView) findViewById(R.id.p_thumb);
        p_name= (TextView) findViewById(R.id.p_name);
        p_price= (TextView) findViewById(R.id.p_price);
        p_traintime= (TextView) findViewById(R.id.p_traintime);
        p_place= (TextView) findViewById(R.id.p_place);
        p_num= (TextView) findViewById(R.id.p_number);
        p_pay= (TextView) findViewById(R.id.p_pay);
        p_dingdan= (TextView) findViewById(R.id.p_dingdan);
        p_updatetime= (TextView) findViewById(R.id.p_updatetime);

        p_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Retrofit retrofit=new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create()).baseUrl(Config.URL).build();
                ActImpls services=retrofit.create(ActImpls.class);
                Call<Integer> call=services.getDingDanTui(p_id,p_type,SessionID);
                call.enqueue(new Callback<Integer>() {
                    @Override
                    public void onResponse(Call<Integer> call, Response<Integer> response) {
                        if (response.body()==1) {
                            Toast.makeText(DingDanActivity.this, "申请退款成功", Toast.LENGTH_LONG).show();
                        }else {
                            Toast.makeText(DingDanActivity.this, "穷鬼，还想退款", Toast.LENGTH_LONG).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<Integer> call, Throwable t) {

                    }
                });
            }
        });



        Retrofit retrofit=new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create()).baseUrl(Config.URL).build();
        ActImpls services=retrofit.create(ActImpls.class);
        Call<Pdingdan> call=services.getPdingdaninfo(p_id,p_type,SessionID);
        call.enqueue(new Callback<Pdingdan>() {
            @Override
            public void onResponse(Call<Pdingdan> call, Response<Pdingdan> response) {
                Pdingdan pdingdan=response.body();
                if (pdingdan.getVstate().equals("0")){
                    ddd_ddd.setText("未参加订单");
                    p_button.setText("申请退款");
                }else if (pdingdan.getVstate().equals("1")){
                    ddd_ddd.setText("已参加订单");
                    p_button.setText("不能退款");
                }else if (pdingdan.getVstate().equals("3")){
                    ddd_ddd.setText("申请退款订单");
                    p_button.setText("已退款");
                }else {
                    ddd_ddd.setText("已退款订单");
                    p_button.setText("已退款");
                }
                p_name.setText(pdingdan.getName());
                p_price.setText(pdingdan.getPrice());
                p_traintime.setText(pdingdan.getTraintime());
                p_place.setText(pdingdan.getPlace());
                p_num.setText(pdingdan.getAdultnum());
                p_pay.setText(pdingdan.getPay());
                p_dingdan.setText(pdingdan.getId());
                p_updatetime.setText(pdingdan.getUpdate_time());
                Picasso.with(DingDanActivity.this).load(Config.PIC_URL+pdingdan.getThumb()).into(p_pic);
            }

            @Override
            public void onFailure(Call<Pdingdan> call, Throwable t) {

                Toast.makeText(DingDanActivity.this,"获取详情失败",Toast.LENGTH_LONG).show();
            }
        });
    }
}
